/**
 * May 12, 2014 9:32:05 AM
 */
package elitech.vietnam.copyweatherfashion.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import elitech.vietnam.copyweatherfashion.R;

/**
 * @author Cong
 *
 */
public class ConfirmDialogFragment extends DialogFragment implements View.OnClickListener {
	
	public static final String ARG_TYPE = "ARG_TYPE";
	public static final String ARG_MESSAGE = "ARG_MESSAGE";
	
	ConfirmDialogClick mClick;
	
	TextView mContent;
	Button mBtnYes, mBtnNo;
	Type mType;
	
	String mMessage;

	public static enum Type {
		ACTIVITY,
		FRAGMENT
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mType = (Type) getArguments().getSerializable(ARG_TYPE);
		try {
			mClick = (ConfirmDialogClick) ((mType == Type.FRAGMENT) ? getTargetFragment() : getActivity());
		} catch (ClassCastException e) {
			throw new ClassCastException("Target Fragment must implement ConfirmDialogClick interface");
		}
		mMessage = getArguments().getString(ARG_MESSAGE);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.confirm_dialog, container, false);
		
		mContent = (TextView) view.findViewById(R.id.confirmdialog_textMessage);
		mBtnYes = (Button) view.findViewById(R.id.confirmdialog_btnYes);
		mBtnNo = (Button) view.findViewById(R.id.confirmdialog_btnNo);
		
		mBtnYes.setOnClickListener(this);
		mBtnNo.setOnClickListener(this);
		
		mContent.setText(mMessage);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirmdialog_btnYes:
			if (mClick != null)
				mClick.yesClick();
			break;
		case R.id.confirmdialog_btnNo:
			if (mClick != null)
				mClick.noClick();
			break;
		default:
			break;
		}
		dismiss();
	}
	
	@Deprecated
	@Override
	public void show(FragmentManager manager, String tag) {
		super.show(manager, tag);
	}
	
	public void show(FragmentManager manager) {
		super.show(manager, getClass().getName());
	}
	
	public interface ConfirmDialogClick {
		public void yesClick();
		public void noClick();
	}
	
	public static class Builder {
		String mMessage;
		Fragment mFragment;
		Type mType;
		
		public Builder() {}
		
		public Builder setMessage(String message) {
			mMessage = message;
			return this;
		}
		
		public Builder setTargetFragment(Fragment fragment) {
			mFragment = fragment;
			return this;
		}
		
		public Builder setType(Type type) {
			mType = type;
			return this;
		}
		
		public ConfirmDialogFragment build() {
			ConfirmDialogFragment fragment = new ConfirmDialogFragment();
			Bundle bundle = new Bundle();
			bundle.putString(ARG_MESSAGE, mMessage);
			bundle.putSerializable(ARG_TYPE, mType);
			fragment.setArguments(bundle);
			if (mType == Type.FRAGMENT)
				fragment.setTargetFragment(mFragment, 69);
			fragment.setStyle(STYLE_NO_TITLE, R.style.Dialog);
			return fragment;
		}
	}
}
